package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.common.R;
import com.example.pojo.req.CommentReq;
import com.example.service.CommentService;
import com.example.service.impl.CommentServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import static com.example.utils.JsonChangeObject.JsonObject;

@WebServlet("/comment")
public class AddComment extends HttpServlet {


    CommentService commentService = new CommentServiceImpl();
    /**
     * 添加评论列表
     *
     * @return
     * @throws Exception
     */
    public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");

        String token = req.getHeader("token");
        String jsonStr = JsonObject(req);
        CommentReq comment = JSON.parseObject(jsonStr, CommentReq.class);
        R addComment = null;
        try {
            addComment = commentService.addComment(comment, token);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(addComment));
    }
}
